package com.showcai.controller.versions;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Versions;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.VersionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/versions")
@Controller
public class VersionsContorller extends BaseController {

    @Autowired
    VersionsService versionsService;


    @RequestMapping(value = "/versionsList")
    public ModelAndView versionsList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/versions/versionsList");
        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        PageInfo<Versions> list = versionsService.getVersionsList(page,pageSize);
        mov.addObject("pageInfo", list);
        return mov;
    }

    @RequestMapping(value = "/versions")
    public ModelAndView versions(Long tid) {
        ModelAndView mov = new ModelAndView();
        Versions versions = versionsService.selectVersionsById(tid);
        mov.setViewName("/versions/versions");
        mov.addObject("versions",versions);
        return mov;
    }

    @RequestMapping(value = "/modifyVersions")
    @ResponseBody
    public String modifyVersions(Versions versions, @RequestParam(value = "file", required = false)MultipartFile file) {

        if (file != null) {
            try {
                String filePath="showcai/app/";
                filePath = super.putObject(file.getInputStream(), filePath, file.getOriginalFilename());
                versions.setReleaseAddress(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        ResultObj resultObj = new ResultObj();
        versionsService.addVersions(versions);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }
}
